package com.capgemini.diabeo.fragment;

import java.util.ArrayList;
import java.util.List;

import android.support.v4.app.Fragment;
import android.text.Editable;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.capgemini.diabeo.R;
import com.capgemini.diabeo.adapter.DishListAdapter;
import com.capgemini.diabeo.adapter.MenuListAdapter;
import com.capgemini.diabeo.manager.ChangeObserver;
import com.capgemini.diabeo.manager.DishManager;
import com.capgemini.diabeo.manager.SearchManager;
import com.capgemini.diabeo.model.Dish;
import com.capgemini.diabeo.model.Menu;
import com.googlecode.androidannotations.annotations.AfterTextChange;
import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.Background;
import com.googlecode.androidannotations.annotations.Bean;
import com.googlecode.androidannotations.annotations.EFragment;
import com.googlecode.androidannotations.annotations.UiThread;
import com.googlecode.androidannotations.annotations.ViewById;

@EFragment(R.layout.fragment_dish)
public class DishFragment extends Fragment {

	@ViewById
	EditText search;

	@ViewById
	TextView dishCount;

	@ViewById
	TextView carbohydrateCount;

	@ViewById
	TextView lipidCount;

	@ViewById
	TextView iGCount;

	@Bean
	DishListAdapter dishAdapter;
	
	@Bean
	SearchManager searchManager;
	
	@AfterTextChange(R.id.search)
	void afterTextChangedOnSearch(Editable text) {
	 	// Something Here
		searchAsync(text.toString());
	 }
	
	@Background
	void searchAsync(String searchString){
		
		updateListContent(searchString);
	}
	
	@UiThread
	void updateListContent(String text){
		
		searchManager.notifyChange(text);
		
		
	}

	@ViewById
	ListView dishesList;

	@Bean
	DishManager dishManager;

	@AfterViews
	public void afterViews() {
		Menu menu = dishManager.getCurrentMenu();
		carbohydrateCount.setText(String.valueOf(menu.getTotalCarbohydrate()));
		iGCount.setText(String.valueOf(menu.getTotalIG()));
		lipidCount.setText(String.valueOf(menu.getTotalLipid()));
		dishCount.setText(String.valueOf(menu.getMenuDishList().size()));
		List<Dish> dishes = dishManager.retrieveDishByMenuStatus();
		dishAdapter.setDishList(dishes);
		dishesList.setAdapter(dishAdapter);
		
		
		searchManager.registerChangeObserver(new ChangeObserver() {
			
			@Override
			public void onChange(String text) {
				
				DishListAdapter searchAdapter = new DishListAdapter();

				if(!text.equals("")){
				List<Dish> dishs = dishAdapter.getDishList();
				List<Dish> searchList = new ArrayList<Dish>();
				
				for (Dish dish : dishs) {
					
					if(dish.getName().toString().toLowerCase().contains(text)){
						searchList.add(dish);
					}
						
				}	
				searchAdapter.setDishList(searchList);
				dishesList.setAdapter(searchAdapter);
				}else{
					dishesList.setAdapter(dishAdapter);
				}
				
			}
		});
		
	}

}
